package leetcode_1_200;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_92 {
    public static void main(String[] args) {
//        System.out.println(reverseBetween(new ListNode(1, new ListNode(2,
//                new ListNode(3, new ListNode(4, new ListNode(5))))), 2, 4));
//        System.out.println(reverseBetween(new ListNode(5,new ListNode(1,new ListNode(1))), 1, 1));
//        System.out.println(reverseBetween(new ListNode(5), 1, 1));
//        System.out.println(reverseBetween(new ListNode(3,new ListNode(5)), 2, 2));
        //System.out.println(reverseBetween(new ListNode(3,new ListNode(5)), 2, 2));
        System.out.println(reverseBetween(new ListNode(1,new ListNode(2,new ListNode(3))), 2, 3));
        //System.out.println(reverseBetween(new ListNode(3,new ListNode(5,new ListNode(6))), 1, 3));
    }

    private static ListNode reverseBetween(ListNode head, int left, int right) {
        /*ListNode pre = new ListNode(0, head);
        int len2 = right - left + 1;
        ListNode l_r = head;
        ListNode r = head;
        left--;
        right--;
        ListNode pree = head;
        ListNode aft = new ListNode();
        while (left != 0 || right != 0){
            if (left > 0){
                l_r = l_r.next;
                left--;
            }
            if (right > 0){
                r = r.next;
                right--;
            }
        }
        aft = r.next;
        r.next = null;
        ListNode[] nodes = new ListNode[len2];
        ListNode cur = l_r;
        int index = 0;
        while (cur != null){
            nodes[index] = cur;
            index++;
            cur = cur.next;
        }
        for(int i = len2 - 1; i >= 0; i--){
            if (i == 0){
                nodes[i].next = null;
                break;
            }
            nodes[i].next = nodes[i - 1];
        }
        nodes[len2 - 1].next = aft;

        return pre.next;*/
        List<ListNode> nodes = new ArrayList<>();
        while (head != null){
            nodes.add(head);
            head = head.next;
        }
        int len = nodes.size();
        for(int i = left; i < right; i++){
            nodes.get(i).next = nodes.get(i - 1);
        }
        if (left - 2 >= 0 && right < len){
            nodes.get(left - 2).next = nodes.get(right - 1);
            nodes.get(left - 1).next = nodes.get(right);
            return nodes.get(0);
        }else if (left - 2 < 0 && right < len){
            nodes.get(left - 1).next = nodes.get(right);
            return nodes.get(right - 1);
        }else if (left - 2 >= 0){
            nodes.get(left - 2).next = nodes.get(right - 1);
            nodes.get(left - 1).next = null;
            return nodes.get(0);
        } else{
            nodes.get(left - 1).next = null;
            return nodes.get(right - 1);
        }
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return val + " " + next;
        }
    }
}
